﻿// -----------------------------------------------------------------------
// <copyright file="StatusQueryResponse.cs" company="Zen Design Corp">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Zen.BlackBerry.Pap.Parser
{
	using System;
	using System.Collections.Generic;
	using System.Xml;
	using System.Xml.Serialization;
	using Zen.BlackBerry.Common;

	/// <summary>
	/// Represents the response from a status query request message.
	/// </summary>
	public class StatusQueryResponse : PapMessage
	{
		private List<StatusQueryResult> _results;

		/// <summary>
		/// Gets or sets the results.
		/// </summary>
		/// <value>The results.</value>
		[XmlArray(ElementName = "statusquery-result")]
		public List<StatusQueryResult> Results
		{
			get
			{
				if (_results == null)
				{
					_results = new List<StatusQueryResult>();
				}
				return _results;
			}
			set
			{
				_results = value;
			}
		}
	}

	/// <summary>
	/// Represents a result within a <see cref="StatusQueryResponse"/>.
	/// </summary>
	public class StatusQueryResult : ResponseResult
	{
		private AddressList _addresses;

		/// <summary>
		/// Gets the addresses.
		/// </summary>
		/// <value>The addresses.</value>
		public AddressList Addresses
		{
			get
			{
				if (_addresses == null)
				{
					_addresses = new AddressList();
				}
				return _addresses;
			}
		}

		/// <summary>
		/// Gets or sets the event time.
		/// </summary>
		/// <value>The event time.</value>
		public DateTime EventTime
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the state of the message.
		/// </summary>
		/// <value>The state of the message.</value>
		public MessageState MessageState
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the quality of service.
		/// </summary>
		/// <value>The quality of service.</value>
		public QualityOfService QualityOfService
		{
			get;
			set;
		}

		/// <summary>
		/// Generates an object from its XML representation.
		/// </summary>
		/// <param name="reader">The <see cref="T:System.Xml.XmlReader"/> stream from which the object is deserialized.</param>
		public override void ReadXml(XmlReader reader)
		{
			base.ReadXml(reader);
			MessageState = reader.ReadEnum<MessageState>("message-state", MessageState.Unknown);
			EventTime = reader.ReadRimTime("event-time");

			Addresses.Clear();
			Addresses.ReadXml(reader);

			using (XmlReader subElementReader = reader.ReadSubtree())
			{
				if (subElementReader.ReadToDescendant("quality-of-service"))
				{
					XmlSerializer ser = new XmlSerializer(typeof(QualityOfService));
					QualityOfService = (QualityOfService) ser.Deserialize(subElementReader);
				}
			}
		}
	}
}
